package cn.kailangtu.myapplication.view;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.DialogFragment;

// DeleteConfirmationDialog.java
public class DeleteConfirmationDialog extends DialogFragment {
    private OnDeleteListener listener;

    // 定义回调接口
    public interface OnDeleteListener {
        void onDeleteConfirmed();
    }

    @Override
    public void onAttach(@NonNull Context context) {
        super.onAttach(context);
        // 确保宿主 Activity/Fragment 实现了回调接口
        if (getParentFragment() instanceof OnDeleteListener) {
            listener = (OnDeleteListener) getParentFragment();
        } else if (context instanceof OnDeleteListener) {
            listener = (OnDeleteListener) context;
        } else {
            throw new RuntimeException("宿主必须实现 OnDeleteListener");
        }
    }

    @NonNull
    @Override
    public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {
        return new AlertDialog.Builder(requireContext())
                .setTitle("删除确认")
                .setMessage("确定要删除此项吗？")
                .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        listener.onDeleteConfirmed();
                    }
                })
                .setNegativeButton("取消", null)
                .create();
    }
}
